package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionBeans.GestionContactGroupLocal;
import sessionBeans.GestionContactLocal;
import entityBeans.Address;
import entityBeans.Contact;
import entityBeans.ContactGroup;
import entityBeans.Enterprise;
import entityBeans.PhoneNumber;

/**
 * Servlet implementation class NewContactServlet
 */

public class NewContactServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB(name = "ContactBeanEntity", beanInterface = GestionContactLocal.class)
	GestionContactLocal daoContact;

	@EJB(name = "ContactGroupBeanEntity", beanInterface = GestionContactGroupLocal.class)
	GestionContactGroupLocal daoGroup;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewContactServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setAttribute("listGroup", daoGroup.search());

		RequestDispatcher dispatcher = request
				.getRequestDispatcher("pages/addContact.jsp");
		dispatcher.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String firstName = request.getParameter("firstname");
		String lastName = request.getParameter("lastname");
		String email = request.getParameter("email");
		String street = request.getParameter("street");
		String city = request.getParameter("city");
		String zip = request.getParameter("zip");
		String country = request.getParameter("country");
		String phone1 = request.getParameter("phone1");
		String phone2 = request.getParameter("phone2");
		String phone3 = request.getParameter("phone3");
		String creationType = request.getParameter("creationtype");

		String gOther = request.getParameter("other");

		String numSiret = "";
		boolean isContact = true;
		if (creationType.equals("Enterprise")) {
			numSiret = request.getParameter("siret");
			isContact = false;
		}
		// create contact
		Enterprise contact = new Enterprise();
		contact.setFirstName(firstName);
		contact.setLastName(lastName);
		contact.setEmail(email);

		// create address
		Address addr = new Address();
		addr.setStreet(street);
		addr.setCity(city);
		addr.setZip(zip);
		addr.setCountry(country);
		contact.setAddress(addr);

		// create phones
		Set<PhoneNumber> phones = new HashSet<PhoneNumber>();
		PhoneNumber mobile1 = new PhoneNumber();
		mobile1.setPhoneKind(request.getParameter("phonetype1"));
		mobile1.setPhoneNumber(phone1);
		phones.add(mobile1);
		// if (!phone2.equals("")) {
		PhoneNumber mobile2 = new PhoneNumber();
		mobile2.setPhoneKind(request.getParameter("phonetype2"));
		mobile2.setPhoneNumber(phone2);
		phones.add(mobile2);
		// }
		// if (!phone3.equals("")) {
		PhoneNumber mobile3 = new PhoneNumber();
		mobile3.setPhoneKind(request.getParameter("phonetype3"));
		mobile3.setPhoneNumber(phone3);
		phones.add(mobile3);
		// }
		contact.setPhones(phones);

		List<String> groupName = daoGroup.search();
		if (groupName == null) {
			groupName = new ArrayList<String>();
			ContactGroup cgFriend = new ContactGroup();
			cgFriend.setGroupName("Friend");
			ContactGroup cgFamily = new ContactGroup();
			cgFamily.setGroupName("Family");
			ContactGroup cgCol = new ContactGroup();
			cgCol.setGroupName("Colleague");
			daoGroup.addGroup(cgFriend);
			daoGroup.addGroup(cgFamily);
			daoGroup.addGroup(cgCol);

		}
		String[] choix = request.getParameterValues("groups");
		// add contact with form
		if (isContact) {
			Contact c = new Contact();

			c.setFirstName(contact.getFirstName());
			c.setLastName(contact.getLastName());
			c.setEmail(contact.getEmail());
			c.setAddress(contact.getAddress());

			if (gOther != null && !gOther.isEmpty()) {

				ContactGroup grp = new ContactGroup();
				grp.setGroupName(gOther);

				daoGroup.addGroup(grp);

				grp.getContacts().add(c);
				c.getBooks().add(grp);
			}

			c.setPhones(contact.getPhones());
			daoContact.addContact(c);

			if (gOther != null && !gOther.isEmpty())
				daoGroup.updateGroupByName(gOther, c);

			if (choix != null)
				for (String grName : choix) {
					if (!grName.equals("none"))
						daoGroup.updateGroupByName(grName, c);
				}

		} else {
			if (gOther != null && !gOther.isEmpty()) {
				ContactGroup grp = new ContactGroup();
				grp.setGroupName(gOther);
				daoGroup.addGroup(grp);
				grp.getContacts().add(contact);
				contact.getBooks().add(grp);
			}

			contact.setNumSiret(Integer.valueOf(numSiret));
			daoContact.addContact(contact);
			if (gOther != null && !gOther.isEmpty())
				daoGroup.updateGroupByName(gOther, contact);
			if (choix != null)
				for (String grName : choix) {
					if (!grName.equals("none"))
						daoGroup.updateGroupByName(grName, contact);
				}
		}

		request.setAttribute("contact",
				contact.getFirstName() + " " + contact.getLastName());
		RequestDispatcher rd = request
				.getRequestDispatcher("pages/personalPage.jsp");
		rd.forward(request, response);
	}
}
